<?php
/**
 * WCMS 登陆器 只跟用户有关注册、登陆有关 其他无关 判断有无登陆 可以设置cookie
 * 描述 调用了MemberService指定接口  login  register getOneMemberByUsername
 * @author wolf
 * @since 2014-08-02 
 * @version 第4次简化
 *
 */
class AnonymousController extends Action {
	
	//修改了这个 别忘记修改权限管理器的cookie
	const COOKIENAME = 'user';
	/**
	 * 管理员登录口
	 */
	public function admin() {
		
		if (isset ( $_COOKIE [self::COOKIENAME] )) {
			$userInfo = self::getMemberService ()->getOneMemberByUsername ( $_COOKIE [self::COOKIENAME] );
			$this->view ()->assign ( "user", $userInfo );
		}
		$this->view ()->display ( 'file:anonymous/login.tpl' );
	}
	
	/**
	 * 用户注册  接口  调用用户服务
	 * 只检测提交的字段是否合法
	 * @todo 默认用户组未添加
	 */
	public function register() {
		$group = self::getMemberService ()->getMemberGroup ();
		$this->view ()->assign ( "group", $group );
		$this->view ()->display ( 'mysql:register.tpl' );
	
	}
	/**
	 * 用户提交注册
	 */
	public function setRegister() {
		$rs = self::getMemberService ()->register ( $_POST );
		$this->sendNotice ( $rs, null, false );
	}
	
	/**
	 * 普通会员登录
	 */
	public function login() {
		//登录插件注册器
		self::getPluginService ( 'login' )->run ();
		if (isset ( $_COOKIE [self::COOKIENAME] )) {
			$userInfo = self::getMemberService ()->getOneMemberByUsername ( $_COOKIE [self::COOKIENAME] );
			$this->view ()->assign ( "user", $userInfo );
		}
		
		$this->view ()->display ( 'mysql:login.tpl' );
	}
	
	/**
	 * 生成验证码
	 */
	public function captcha() {
		require 'Captcha.php';
		//确保每次都能取到新值
		unset ( $_SESSION ['authum_session'] );
		// 实例化一个对象
		$_vc = new Captcha ();
		$_vc->doimg ();
		$_SESSION ['authnum_session'] = $_vc->getCode ();
	}
	
	/**
	 * 登录验证
	 */
	public function setLogin() {
		$user = self::getMemberService ()->login ( $_POST );
		
		//登陆成功 默认记录10个小时
		if (is_array ( $user )) {
			setcookie ( self::COOKIENAME, $user ['username'], time () + 3600 * 10, "/" );
			self::getLogService ()->add ( $user ['username'], "登录" );
			$user = self::SUCEESS;
		}
		
		$this->sendNotice ( $user, null, false );
	}
	
	/**
	 * 退出登录 同步登录
	 */
	public function signout() {
		self::getLogService ()->add ( $_COOKIE [self::COOKIENAME], "退出" );
		setcookie ( self::COOKIENAME, "", - 86400, "/" );
		setcookie ( "jiami", "", - 86400, "/" );
		
		$this->redirect ( "退出成功!", '/index.php?anonymous/login' );
	}
	
	/**
	 * 获取用户服务类
	 */
	public static function getMemberService() {
		return new MemberService ();
	}
}